package com.wll.ec.fast.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wll.ec.core.app.AccountManager;
import com.wll.ec.fast.database.DatabaseManager;
import com.wll.ec.fast.database.UserProfile;

/**
 * Created by detachment on 2020/3/17
 */
public class SignHandler {

    public static void onSignUp(String response, ISignListener signListener){
        final JSONObject profileJson = JSON.parseObject(response).getJSONObject("data");
        final long userId = profileJson.getLong("userId");
        final String name = profileJson.getString("name");
        final String avatar = profileJson.getString("avatar");
        final String gender = profileJson.getString("gender");
        final String address = profileJson.getString("address");

        final UserProfile profile = new UserProfile(userId, name, avatar, gender, address);
        DatabaseManager.getInstance().getUserProfileDao().insertOrReplace(profile);

        // 注册成功
        AccountManager.setSignState(true);
        if (signListener != null){
            signListener.onSignUpSuccess();
        }
    }

    public static void onSignIn(String response, ISignListener signListener) {
        final JSONObject profileJson = JSON.parseObject(response).getJSONObject("data");
        final long userId = profileJson.getLong("userId");
        final String name = profileJson.getString("name");
        final String avatar = profileJson.getString("avatar");
        final String gender = profileJson.getString("gender");
        final String address = profileJson.getString("address");

        final UserProfile profile = new UserProfile(userId, name, avatar, gender, address);
        DatabaseManager.getInstance().getUserProfileDao().insertOrReplace(profile);

        // 登录成功
        AccountManager.setSignState(true);
        if (signListener != null){
            signListener.onSignInSuccess();
        }
    }

}
